package com.steellee.leeexcel;

import com.steellee.leeexcel.dto.MetaExcelReadDTO;
import com.steellee.leeexcel.dto.PaExampleOneDto;
import com.steellee.leeexcel.dto.PaExampleTwoDto;
import com.steellee.leeexcel.msg.JSONMessage;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PaExcelReadDemo extends Parent {
    @Autowired
    private PaExcelRead operate;

    public FileInputStream getTureInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template"
                + System.getProperty("file.separator") + "excelAnalysisTrue.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    /**
     * param l inputStream excel文件流 必传
     * param2 List<MetaExcelDTO> 入参数据对象数组 必传
     * param2.1 resultClazz 指定返回对象名.class 必传
     * param2.2 allowMaxRows 用于最大行数校验 非必传
     * param2.3 sheetNo 指定读取哪个 sheet 非必传
     * param2.4 dataStartRowNo 指定从哪一行读起 非必传
     * param2.5 isCheck true: 使用 validate校验, false/ null: 不做 validate校验 非必传
     * <p>
     * result Map<String, Object>--> KEY为返回对象名, VALUE为返回对象集合
     */
    @Test
    public void readForObjSuccess() {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setResultClazz(PaExampleOneDto.class);
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setResultClazz(PaExampleTwoDto.class);
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        param.add(dto2);
        JSONMessage result = operate.readForObj(getTrueInputStream(), param);
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    /**
     * param l inputStream excel文件流 必传
     * ^ param2 List<MetaExcelDTO>入参数据对象数组 必传
     * ^ param2.1 resultClazz 指定返回对象名.class 必传
     * param2.2 allowMaxRows 用于最大行数校验 非必传
     * ^ param2.3 sheetNo 指定读取哪个 sheet 非必传
     * param2.4 dataStartRowNo 指定从哪一行读起 非必传
     * param3 outputStream 返回文件流
     */

    @Test
    public void readForObjFailReturnNoTemp() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setResultClazz(PaExampleOneDto.class);
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setResultClazz(PaExampleTwoDto.class);
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        param.add(dto2);
        JSONMessage msg = operate.readForObj(getFalseInputStream(), param);
        Map<String, Object> data = (Map<String, Object>) msg.result;
        JSONMessage result = operate.objDataCheck(param, data, null, null);
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    /**
     * param l inputStream excel文件流 必传
     * param2 List<MetaExcelDTO> 入参数据对象数组 必传
     * param2.1 resultClazz 指定返回对象名.class 必传
     * param2.2 allowMaxRows 用于最大行数校验 非必传
     * param2.3 sheetNo 指定读取哪个 sheet 非必传
     * param2.4 dataStartRowNo 指定从哪一行读起 非必传
     * param2.5 abstractClazz 继承组件抽象类,用于实现个性化检验,非必传
     * param3 outputStream 返回文件流
     */

    @Test
    public void readForObjFailReturnWithOwnCheck() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setResultClazz(PaExampleOneDto.class);
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        dto1.setAbstractClazz(OwnObjCheck.class);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setResultClazz(PaExampleTwoDto.class);
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        dto2.setAbstractClazz(OwnObjCheck.class);
        param.add(dto2);
        JSONMessage msg = operate.readForObj(getFalseInputStream(), param);
        Map<String, Object> data = (Map<String, Object>) msg.result;
        JSONMessage result = operate.objDataCheck(param, data, null, getObjTemplate());
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }

    /**
     * 获取没问题的 excel文件
     *
     * @return fileInputStream
     * @ throws IOException
     */
    public FileInputStream getTrueInputStream() {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "excelAnalysisTrue.xlsx";
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileInputStream;
    }

    /**
     * 获取有问题的 excel文件
     *
     * @return fileInputStream
     * @ throws IOException
     */
    public FileInputStream getFalseInputStream() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "excelAnalysisFalse.xlsx";
        FileInputStream ins = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return ins;
    }

    /**
     * 获取模板
     *
     * @return fileInputStream
     * @ throws IOException
     */
    public FileInputStream getObjTemplate() throws IOException {
        String excelPath = System.getProperty("file.separator") + "template" + System.getProperty("file.separator") + "objTemplate.xlsx";
        FileInputStream fileInputStream = new FileInputStream((new ClassPathResource(excelPath)).getFile());
        return fileInputStream;
    }

    /**
     * param l inputstream excel文件流 必传
     * +  param2 List<MetaExcelDTO> 入参数据对象数组 必传
     * π  param2.1 resultClazz 指定返回对象名.class 必传
     * param2.2 allowMaxRows 用于最大行数校验 非必传
     * ^  param2.3 sheetNo 指定读取哪个 sheet 非必传
     * param2.4 dataStartRowNo 指定从哪一行读起 非必传
     * param3 outputStream 返回文件流
     */

    @Test
    public void readForObjFailReturn() throws Exception {
        List<MetaExcelReadDTO> param = new ArrayList<>();
        MetaExcelReadDTO dto1 = new MetaExcelReadDTO();
        dto1.setResultClazz(PaExampleOneDto.class);
        dto1.setDataStartRowNo(1);
        dto1.setSheetNo(0);
        param.add(dto1);
        MetaExcelReadDTO dto2 = new MetaExcelReadDTO();
        dto2.setResultClazz(PaExampleTwoDto.class);
        dto2.setDataStartRowNo(1);
        dto2.setSheetNo(1);
        param.add(dto2);
        JSONMessage msg = operate.readForObj(getFalseInputStream(), param);
        Map<String, Object> data = (Map<String, Object>) msg.result;
        JSONMessage result = operate.objDataCheck(param, data, null, getObjTemplate());
        System.out.println("1返回码: " + result.responseCode + ", 返回信息: " + result.responseMsg);
    }
}